// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 70 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function162

// Static Values

// Tables
public table Eggman_table54
	7, 8, 8, 7, 7, 16
end table


function Eggman_Function162
	object.ypos = object.value1
	Sin256(temp0, object.value2)
	temp0 <<= 10
	object.ypos += temp0
	object.value2 += 2
	object.value2 &= 255
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
		temp0 = object.ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		if object[0].xpos > object.xpos
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.value1 = object.ypos
			object[+1].type = TypeName[Chemical Dropper]
			object[+1].value4 = object.xpos
			object[+1].value4 -= 0x700000
			object[+1].value5 = object.xpos
			object[+1].value5 += 0x700000
			temp0 = screen.xcenter
			temp0 += 256
			temp0 <<= 16
			object.xpos += temp0
			object[+1].xpos = object.xpos
			object[+1].ypos = object.ypos
			object[+1].value0 = object.xpos
			object[+1].value1 = object.ypos
			object[+1].priority = PRIORITY_ACTIVE
			object.value5 = 1
			object.value7 = 8
			object.priority = PRIORITY_ACTIVE
			object.state++
		end if
		break
	case 1
		break
	case 2
		object.value4++
		if object.value4 == 180
			object.value4 = 0
			object.animation = 5
			object.value9 = 0
			object.state++
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 3
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.value4++
		if object.value4 == 38
			object.yvel = 0
			object.value4 = 0
			object.animationTimer = 0
			object.state++
		end if
		break
	case 4
		if object.value4 < 48
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.value4++
		else
			object.value4 = 0
			object.yvel = 0
			object.value1 = object.ypos
			object.state++
		end if
		break
	case 5
		object.ypos = object.value1
		CallFunction(Eggman_Function162)
		if object.value4 < 8
			object.value4++
		else
			object.value4 = 0
			object.animation = 6
			object.animationTimer = 0
			object.value5 = 2
			object.value6 = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 6
		object.xpos += 0x40000
		object.value1 -= 0x4000
		CallFunction(Eggman_Function162)
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		break
	end switch
	if object[0].gravity == GRAVITY_GROUND
		object.value10 = 0
	end if
	if object.value7 != 0
		if object.value8 > 0
			object.value8--
			GetBit(temp0, object.value8, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value8 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 22, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == true
					CallFunction(PlayerObject_CheckHit)
					if checkResult == true
						object.value7--
						if object.value7 == 0
							player.score += 1000
							object.animation = 4
							object.animationTimer = 0
							object.value5 = 0
							object.value9 = 1
							object.value1 = object.ypos
							object.value2 = object[+1].value2
							object.state++
							object[+1].state = 4
							if object.value10 >= 7
								if stage.debugMode == 0
									CallNativeFunction2(SetAchievement, 5, 100)
								end if
							end if
						else
							object.animation = 3
							object.animationTimer = 0
							object.value8 = 32
							PlaySfx(SfxName[Boss Hit], 0)
							if currentPlayer == 0
								if object[0].gravity == GRAVITY_AIR
									object.value10++
								end if
							end if
						end if
					end if
				end if
			end if
		next
	end if
	if object.state != 6
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value9 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	if object.animation != 0
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	end if
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(15, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	end switch
	switch object.value5
	case 1
		GetTableValue(temp0, object.value6, Eggman_table54)
		object.value6++
		if object.value6 == 6
			object.value6 = 0
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.value6
		temp0 /= 3
		temp0 += 9
		object.value6++
		if object.value6 >= 18
			object.value6 = 0
			temp0 = 7
			object.value5 = 1
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == 1
		if saveRAM[495] == 0
			LoadSpriteSheet("Global/EggMobile.gif")
		else
			LoadSpriteSheet("Global/EggRoboMobile.gif")
		endif
		SpriteFrame(-28, -28, 60, 20, 5, 1)
		SpriteFrame(-28, -28, 60, 20, 66, 1)
		SpriteFrame(-28, -28, 60, 20, 5, 22)
		SpriteFrame(-28, -28, 60, 20, 66, 22)
		SpriteFrame(-28, -28, 60, 20, 5, 43)
		SpriteFrame(-28, -28, 60, 20, 66, 43)
		SpriteFrame(-32, -8, 64, 29, 1, 64)
		SpriteFrame(31, 4, 12, 8, 66, 65)
		SpriteFrame(30, 2, 14, 12, 66, 74)
		SpriteFrame(31, -2, 22, 14, 66, 87)
		SpriteFrame(31, -9, 32, 26, 94, 64)
		SpriteFrame(31, -2, 22, 14, 66, 87)
		SpriteFrame(31, -9, 32, 26, 94, 64)
		SpriteFrame(31, 4, 12, 8, 66, 65)
		SpriteFrame(30, 2, 14, 12, 66, 74)
		SpriteFrame(-3, -28, 11, 8, 79, 65)
		SpriteFrame(0, 0, 0, 0, 7, 2)
		if options.vsMode == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -28, 60, 20, 1, 1)
		SpriteFrame(-28, -28, 60, 20, 62, 1)
		SpriteFrame(-28, -28, 60, 20, 1, 22)
		SpriteFrame(-28, -28, 60, 20, 62, 22)
		SpriteFrame(-28, -28, 60, 20, 1, 43)
		SpriteFrame(-28, -28, 60, 20, 62, 43)
		SpriteFrame(-32, -8, 64, 29, 415, 170)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(-3, -28, 11, 8, 45, 88)
		SpriteFrame(0, 0, 0, 0, 7, 2)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
